﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ThoiTrang.Business;
using ThoiTrang.Entities;

public partial class Admin_genRSS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.createRSS();
        }
        Response.Redirect("adm_article_list.aspx?st=0&ckid=" + SessionManager.GetCookie());
    }
     
    void createRSS()
    {
        DataSet ds = a_ArticleService.a_Article_RSS_allsite();
        if (ds.Tables.Count > 0)
        {
            foreach (DataRow dtr in ds.Tables[0].Rows)
            {
                generateRSS("RSS-" + dtr["CatID"].ToString() + ".xml", 
                    "1",
                    dtr["CatID"].ToString(), 
                    "9",
                    dtr["uName"].ToString());
            }
        }
    }

    void generateRSS(string fileName, string typeCat,  string catID, string numTop,string  desTitle)
    {
        DataSet ds = a_ArticleService.a_Article_RSS(Convert.ToInt32( typeCat), catID.ToString(), numTop.ToString());
        string xml = "<rss version=\"2.0\">\r\n<channel>\r\n" +
            "<title>Thoitrang.com " + desTitle + "</title>\r\n" +
            "<link>http://thoitrang.com</link>\r\n" +
            "<description>thoi trang .com ra doi nham cung cap noi dung moi nhat cua thoi trang, cap nhat thong tin thuong xuyen tinh hinh thoi trang viet nam cung nhu phan tich khuynh huong thoi trang tre hay nhat</description>\r\n" +
            "<language>vi-vn</language>\r\n" +
            "<powerby>ThoiTrang.com</powerby>\r\n" +
            "<lastBuildDate>" + DateTime.Now.ToString(FOConstants.DBDateTimeFormat) + "</lastBuildDate>\r\n";
          

        string siteUrl = "http://thoitrang.com/";

        if (ds.Tables.Count > 0)
        {
            foreach (DataRow dtr in ds.Tables[0].Rows)
            {
                //System.Web.HttpUtility.HtmlEncode(dtr[""].ToString());
                xml +=
                "<item>" +
                "<title>" + System.Web.HttpUtility.HtmlEncode(dtr["uTitle"].ToString()) + "</title>" +
                "<link>" + System.Web.HttpUtility.HtmlEncode("http://thoitrang.com/detail.aspx?aid="+ dtr["ID"].ToString()) + "</link>" +
                "<description>" + System.Web.HttpUtility.HtmlEncode(dtr["uBody"].ToString()) + "</description>" +
                "<image >" +
                "	<link>"+ System.Web.HttpUtility.HtmlEncode("http://thoitrang.com/detail.aspx?aid="+dtr["ID"].ToString()) + "</link>" +
                "	<url>" +System.Web.HttpUtility.HtmlEncode("http://thoitrang.com/"+FOConstants.Dir_ImgArticle +DetailImageName( dtr["ID"],dtr["kTitle"],dtr["fYear"],dtr["ImageNo"])) +  "</url>" +
                "</image>" +
                "</item>\r\n";
            }
        }
        xml += "</channel>\r\n</rss>";

        try
        {
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName, false);
            sw.Write(xml);
            sw.Flush();
            sw.Close();
        }
        catch
        { }

    }
    public string DetailImageName(object aID, object kTitle, object fYear, object imgNo)
    {
        string res = "";
        res = kTitle.ToString().ToLower().Trim().Replace(" ", "-");
        res = res + "-" + fYear.ToString() + "-d" + aID.ToString() + imgNo.ToString() + ".jpg";
        return res;
    }

}
